//
//  AddDPDetailTableViewController.m
//  TimeManager
//
//  Created by 刘凯敏 on 19/12/2016.
//  Copyright © 2016 刘凯敏. All rights reserved.
//

#import "AddDPDetailTableViewController.h"

@interface AddDPDetailTableViewController ()

@end

@implementation AddDPDetailTableViewController
@synthesize addTableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectIndex = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //选中之后的cell的高度
    if(indexPath.row == self.selectIndex.row && self.selectIndex != nil){
        return 150;
    }
    else return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier_ = @"cell_";
    static NSString *identifier = @"cell";
    //判断cell是否处于选中状态，并且用不同的identifier标记重用
    //选中的cell改变高度，同时在cell的下方添加选择器
    if(indexPath.row == 0){
        //选中状态
        if (indexPath.row == self.selectIndex.row && self.selectIndex != nil){
            UITableViewCell *cell_ = [tableView dequeueReusableCellWithIdentifier:identifier_];
            if (cell_ == nil){
                cell_ = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier_];
            }
            
            UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn1.frame = CGRectMake(20, 75, 80, 30);
            [btn1 setTitle:@"leftBtn" forState:UIControlStateNormal];
            [cell_.contentView addSubview:btn1];
            //cell_.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell_;
        }
        else {   //非选中状态
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil){
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            //cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    //else if(indexPath.row == 1){
    //}
    else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        return cell;
    }
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //有时在push，back回来之后，当前cell仍然是选中的状态
    //解决方法添加如下一行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 0){
        if (!self.selectIndex){
            self.selectIndex = indexPath;
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.selectIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        else{
            //若点击相同的cell，收起cell
            if (indexPath.row == self.selectIndex.row){
                self.selectIndex = nil;
                [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
        }
    }
    [addTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
